package GUIs;

import Configs.Config;
import Controllers.GeneratetPDF;
import Controllers.HoaDonController;
import Controllers.NhanVienController_2;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import models.NhanVien;

public class f_nhanvien_nguoithue_hoadon_them extends javax.swing.JFrame {

    private static f_nhanvien_nguoithue_hoadon_them _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private String msnv;
    public String maHD;
    private HoaDonController hoaDonControl;
    private NhanVienController_2 nhanVienControl;
    public NhanVien nhanVienThem;
    private GeneratetPDF genPDF;

    public f_nhanvien_nguoithue_hoadon_them(Config conn) {
        this.conn = conn;
        initComponents();
    }

    public static f_nhanvien_nguoithue_hoadon_them getInstance(Config conn, String msnv, String maHD) {
        if (_instance == null) {
            _instance = new f_nhanvien_nguoithue_hoadon_them(conn);
        }
        _instance.msnv = msnv;
        _instance.maHD = maHD;
        _instance.resetForm();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnchinh = new javax.swing.JPanel();
        lbhopdong = new javax.swing.JLabel();
        lbhopdong_tt = new javax.swing.JLabel();
        lbhoadon = new javax.swing.JLabel();
        lbhoadon_tt = new javax.swing.JLabel();
        spghichu = new javax.swing.JScrollPane();
        textareaghichu = new javax.swing.JTextArea();
        lbghichu = new javax.swing.JLabel();
        cbbtuthang = new javax.swing.JComboBox();
        lbtuthang = new javax.swing.JLabel();
        lbdenthang = new javax.swing.JLabel();
        cbbdenthang = new javax.swing.JComboBox();
        btnlaphoadon = new javax.swing.JButton();
        btnlamlai = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        lbnguoilap = new javax.swing.JLabel();
        lbnguoilap_tt = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm hóa đơn người thuê");
        setPreferredSize(new java.awt.Dimension(620, 420));
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbhopdong.setText("Mã hợp đồng:");

        lbhopdong_tt.setText("HD0002");

        lbhoadon.setText("Mã hóa đơn:");

        lbhoadon_tt.setText("HOADON0002");

        textareaghichu.setColumns(20);
        textareaghichu.setRows(5);
        spghichu.setViewportView(textareaghichu);

        lbghichu.setText("Ghi chú:");

        cbbtuthang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chọn", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        lbtuthang.setText("Từ tháng:");

        lbdenthang.setText("Đến tháng:");

        cbbdenthang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chọn", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        btnlaphoadon.setText("Lập hóa đơn");
        btnlaphoadon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlaphoadonActionPerformed(evt);
            }
        });

        btnlamlai.setText("Làm lại");
        btnlamlai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlamlaiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        lbnguoilap.setText("Người lập:");

        lbnguoilap_tt.setText("(1111) Phan Lâm Mỹ Ngà");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("THÊM HOÁ ĐƠN");

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnchinhLayout.createSequentialGroup()
                                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lbghichu)
                                    .addComponent(lbhoadon)
                                    .addComponent(lbhopdong)
                                    .addComponent(lbtuthang)
                                    .addComponent(lbnguoilap))
                                .addGap(35, 35, 35)
                                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbnguoilap_tt)
                                    .addGroup(pnchinhLayout.createSequentialGroup()
                                        .addComponent(cbbtuthang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(124, 124, 124)
                                        .addComponent(cbbdenthang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(lbhopdong_tt)
                                    .addComponent(lbhoadon_tt)
                                    .addComponent(spghichu, javax.swing.GroupLayout.PREFERRED_SIZE, 449, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(pnchinhLayout.createSequentialGroup()
                                .addGap(186, 186, 186)
                                .addComponent(lbdenthang))))
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addGap(188, 188, 188)
                        .addComponent(btnlaphoadon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnlamlai)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnthoat)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbhopdong, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbhopdong_tt, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbhoadon)
                    .addComponent(lbhoadon_tt))
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(lbghichu))
                    .addGroup(pnchinhLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(spghichu, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbdenthang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbbtuthang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lbtuthang)
                        .addComponent(lbdenthang)))
                .addGap(18, 18, 18)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbnguoilap)
                    .addComponent(lbnguoilap_tt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnlaphoadon)
                    .addComponent(btnlamlai)
                    .addComponent(btnthoat))
                .addGap(24, 24, 24))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnchinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnlaphoadonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlaphoadonActionPerformed
        if (this.hoaDonControl.Add(this)) {
            this.resetForm();
            f_nhanvien_nguoithue_qlhopdong qlhopdong = f_nhanvien_nguoithue_qlhopdong.getInstance(this.conn, this.msnv, this.nhanVienThem.getCMND());
            qlhopdong.refreshDsHoaDon();
            qlhopdong.requestFocus();
            this.resetForm();
            this.dispose();
        }
    }//GEN-LAST:event_btnlaphoadonActionPerformed

    private void btnlamlaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlamlaiActionPerformed
        this.resetForm();
    }//GEN-LAST:event_btnlamlaiActionPerformed

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void resetForm() {
        this.hoaDonControl = new HoaDonController(this.conn);
        this.nhanVienControl = new NhanVienController_2(this.conn);
        this.nhanVienThem = this.nhanVienControl.getWithMSNV(msnv);
        this.lbhopdong_tt.setText(maHD);
        this.lbhoadon_tt.setText(this.hoaDonControl.generateMaHoaDon());
        this.lbnguoilap_tt.setText("(" + this.nhanVienThem.getMSNV() + ") " + this.nhanVienThem.getHoten());
        this.textareaghichu.setText(null);
        this.cbbtuthang.setSelectedIndex(0);
        this.cbbdenthang.setSelectedIndex(0);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlamlai;
    private javax.swing.JButton btnlaphoadon;
    private javax.swing.JButton btnthoat;
    public javax.swing.JComboBox cbbdenthang;
    public javax.swing.JComboBox cbbtuthang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbdenthang;
    private javax.swing.JLabel lbghichu;
    private javax.swing.JLabel lbhoadon;
    public javax.swing.JLabel lbhoadon_tt;
    private javax.swing.JLabel lbhopdong;
    public javax.swing.JLabel lbhopdong_tt;
    private javax.swing.JLabel lbnguoilap;
    private javax.swing.JLabel lbnguoilap_tt;
    private javax.swing.JLabel lbtuthang;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JScrollPane spghichu;
    public javax.swing.JTextArea textareaghichu;
    // End of variables declaration//GEN-END:variables
}
